<?php
require_once(dirname(__FILE__).'/../../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('dataaccess.wpdatex');

require_once(dirname(__FILE__).'/Testers.php');

class TestSimpleDatexM extends WpTestSuite
{
  private $_host = 'localhost';
  private $_user = 'wp';
  private $_password = 'wp';
  private $_db = 'wp';
  private $_table = 'tester';
  
  /**
   * Preparation
   */
  public function prepare()
  {   
    $this->addMessage('Using database host: '.$this->_host);
    $this->addMessage('Using database: '.$this->_db);
    $this->addMessage('Using user: '.$this->_user);
    $this->addMessage('Using database table: '.$this->_table);
    
    $this->addMessage('Connect to database...');
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      die;
    }
    $this->addMessage('... established!');
    
    $this->addMessage('Has database?');
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('Database not found. Try creating...');      
      if (!mysql_query('CREATE DATABASE '.$this->_db)) {
        $this->addMessage('... not successfull. Test stopped.');
        die;
      } else {
        $this->addMessage('... successfull.');
        mysql_select_db($this->_db, $c);    
      }
    } else {
      $this->addMessage('Database found'); 
    }
    
    $r = mysql_query('SHOW TABLES');
    $found = false;
    while ($row = mysql_fetch_array($r)) {
      if ($row[0] === $this->_table) {
        $found=true; break;
      }
    }
    mysql_free_result($result);
    
    if (!$found) {
      if (!mysql_query('CREATE TABLE  `'.$this->_table.'` ('.
        'id int(11) NOT NULL AUTO_INCREMENT,'.
        'field1 varchar(255) NOT NULL,'.
        'field2 double NULL,'.
        'field3 text NULL,'.
        'PRIMARY KEY (id)'.
        ') ENGINE=MyISAM DEFAULT CHARSET=latin1'
      )) {
        $this->addMessage('Table not found. Try to create ... failed. Test stopped.');
        die;
      } else {
        $this->addMessage('Table not found. Try to create ... successfull.');
      }
    } else {
      $this->addMessage('Table found.');
    }
    
    if (!mysql_query('TRUNCATE '.$this->_table)) {
      $this->addMessage('Trying to empty the table ... failed. Test stopped.');
      die;
    }
    
    $this->addMessage('Trying to empty the table ... successfull.');
    return true;
  }
  
  /**
   * Test
   */
  public function test()
  {
    /* Single requests */
    $l = new WpMemLog();
    
    $c = new WpConnectMySQL(array(
      'location' => $this->_host.'/'.$this->_db,
      'user' => $this->_user,
      'password' => $this->_password,
      'log' => $l
    ));
    
    $o = new Testers($c, $l);
    
    $this->addMessage('Adding datasets');
    
    for ($i = 0; $i < 1000; $i++) {
      $o->add(array(
        'field1' => 'Dataset'.strval($i), 'field2' => $i+0.1, 'field3' => 'Body'.strval($i)
      ));
    }
    $this->addResult('add -> save', true, $o->save(), '===');
    $this->addResult('length', 1000, $o->length(), '===');
    
    $this->addMessage('Load all, findNext');
    $o->reset();
    $this->addResult('load all', 1000, $o->load(), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 1000, $o->length(), '===');
    $o->rewind();
    $this->addResult('dataset findNext', true, $o->findNext(), '===');
    $this->addResult('dataset1 - field1', 'Dataset0', $o->get('field1'), '===');
    $this->addResult('dataset1 - field2', 0.1, $o->get('field2'), '===');
    $this->addResult('dataset1 - field3', 'Body0', $o->get('field3'), '===');
    $this->addResult('dataset findNext', true, $o->findNext(), '===');
    $this->addResult('dataset2 - field1', 'Dataset1', $o->get('field1'), '===');
    $this->addResult('dataset2 - field2', 1.1, $o->get('field2'), '===');
    $this->addResult('dataset2 - field3', 'Body1', $o->get('field3'), '===');
    $this->addResult('dataset findNext', true, $o->findNext('field2', 10.1), '===');
    $this->addResult('dataset11 - field1', 'Dataset10', $o->get('field1'), '===');
    $this->addResult('dataset11 - field2', 10.1, $o->get('field2'), '===');
    $this->addResult('dataset11 - field3', 'Body10', $o->get('field3'), '===');
    
    $this->addMessage('Load limit, sort');
    $o->reset();
    $this->addResult('load first 100', 100, $o->load(null, 'field2', 'ASC', 100, 0), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 100, $o->length(), '===');
    $o->rewind();
    $this->addResult('dataset findNext', true, $o->findNext(), '===');
    $this->addResult('dataset1 - field1', 'Dataset0', $o->get('field1'), '===');
    $this->addResult('dataset1 - field2', 0.1, $o->get('field2'), '===');
    $this->addResult('dataset1 - field3', 'Body0', $o->get('field3'), '===');
    $this->addResult('dataset 10 findNext', true, $o->findNext('field2', 10.1), '===');
    $this->addResult('dataset11 - field1', 'Dataset10', $o->get('field1'), '===');
    $this->addResult('dataset11 - field2', 10.1, $o->get('field2'), '===');
    $this->addResult('dataset11 - field3', 'Body10', $o->get('field3'), '===');
    
    $this->addResult('load next 100', 100, $o->load(null, 'field2', 'ASC', 100, 100), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 100, $o->length(), '===');
    $o->rewind();
    $this->addResult('dataset findNext', true, $o->findNext(), '===');
    $this->addResult('dataset100 - field1', 'Dataset100', $o->get('field1'), '===');
    $this->addResult('dataset100 - field2', 100.1, $o->get('field2'), '===');
    $this->addResult('dataset100 - field3', 'Body100', $o->get('field3'), '===');
    $this->addResult('dataset 110 findNext', true, $o->findNext('field2', 110.1), '===');
    $this->addResult('dataset110 - field1', 'Dataset110', $o->get('field1'), '===');
    $this->addResult('dataset110 - field2', 110.1, $o->get('field2'), '===');
    $this->addResult('dataset110 - field3', 'Body110', $o->get('field3'), '===');
    $this->addResult('dataset 500 findNext', false, $o->findNext('field2', 500.1), '===');
    
    $this->addResult('load last 50', 50, $o->load(null, 'field2', 'ASC', 100, 950), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 50, $o->length(), '===');
    $o->rewind();
    $this->addResult('dataset findNext', true, $o->findNext(), '===');
    $this->addResult('dataset950 - field1', 'Dataset950', $o->get('field1'), '===');
    $this->addResult('dataset950 - field2', 950.1, $o->get('field2'), '===');
    $this->addResult('dataset950 - field3', 'Body950', $o->get('field3'), '===');
    $this->addResult('dataset 999 findNext', true, $o->findNext('field2', 999.1), '===');
    $this->addResult('dataset999 - field1', 'Dataset999', $o->get('field1'), '===');
    $this->addResult('dataset999 - field2', 999.1, $o->get('field2'), '===');
    $this->addResult('dataset999 - field3', 'Body999', $o->get('field3'), '===');
    $this->addResult('dataset findNext', false, $o->findNext(), '===');
    $this->addResult('load more than in db', 0, $o->load(null, 'field2', 'ASC', 100, 1000), '===');
    
    $this->addResult('load sort desc', 100, $o->load(null, 'field2', 'DESC', 100, 0), '===');
    $this->addResult('dataset 999 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset999 - field1', 'Dataset999', $o->get('field1'), '===');
    $this->addResult('dataset999 - field2', 999.1, $o->get('field2'), '===');
    $this->addResult('dataset999 - field3', 'Body999', $o->get('field3'), '===');
    $this->addResult('dataset 998 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset998 - field1', 'Dataset998', $o->get('field1'), '===');
    $this->addResult('dataset998 - field2', 998.1, $o->get('field2'), '===');
    $this->addResult('dataset998 - field3', 'Body998', $o->get('field3'), '===');
    
    $this->addMessage('Load, add, modifiy, save');
    $o->reset();
    $this->addResult('load first 100', 100, $o->load(null, 'field2', 'ASC', 100, 0), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 100, $o->length(), '===');
    $this->addResult('dataset 0 findNext', true, $o->findNext(), '===');
    $o->set('field1', 'Dataset0_Modified');
    $o->set('field3', 'Body_Modified', true);
    $o->add(array(
      'field1' => 'Dataset1000',
      'field2' => 1000.1,
      'field3' => 'Body1000'
    ));
    $this->addResult('save', true, $o->save(), '===');
    $this->addResult('length', 101, $o->length(), '===');
    
    $o->reset();
    $this->addResult('load first 100', 100, $o->load(null, 'field2', 'ASC', 100, 0), '===');
    $this->addResult('dataset0 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset0 - field1', 'Dataset0_Modified', $o->get('field1'), '===');
    $this->addResult('dataset0 - field2', 0.1, $o->get('field2'), '===');
    $this->addResult('dataset0 - field3', 'Body_Modified', $o->get('field3'), '===');
    $this->addResult('dataset1 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset1 - field1', 'Dataset1', $o->get('field1'), '===');
    $this->addResult('dataset1 - field2', 1.1, $o->get('field2'), '===');
    $this->addResult('dataset1 - field3', 'Body_Modified', $o->get('field3'), '===');
    
    $o->reset();
    $this->addResult('load last 1', 1, $o->load(null, 'field2', 'ASC', 100, 1000), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 1, $o->length(), '===');
    $this->addResult('dataset1000 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset1000 - field1', 'Dataset1000', $o->get('field1'), '===');
    $this->addResult('dataset1000 - field2', 1000.1, $o->get('field2'), '===');
    $this->addResult('dataset1000 - field3', 'Body1000', $o->get('field3'), '===');
    
    $this->addMessage('Remove');
    $o->reset();
    $this->addResult('load first 100', 100, $o->load(null, 'field2', 'ASC', 100, 0), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 100, $o->length(), '===');
    $this->addResult('remove', 100, $o->remove(), '===');
    
    $o->reset();
    $this->addResult('load first 100', 100, $o->load(null, 'field2', 'ASC', 100, 0), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('length', 100, $o->length(), '===');
    $this->addResult('dataset0 findNext', true, $o->findNext(), '===');
    $this->addResult('dataset0 - field1', 'Dataset100', $o->get('field1'), '===');
    $this->addResult('dataset0 - field2', 100.1, $o->get('field2'), '===');
    $this->addResult('dataset0 - field3', 'Body100', $o->get('field3'), '===');
  }

  
  /**
   * Cleanup
   */
  public function cleanup()
  {
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      return;
    }
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('DB selection failed. Test stopped.');
      return;
    }
    $r = mysql_query('TRUNCATE '.$this->_table);
    if (!$r) {
      $this->addMessage('Table could not be truncated.');
      return;
    }
    
    $this->addMessage('Successfully truncated table.');
  }
  
  
}

$t = new TestSimpleDatexM();
$t->start('SimpleDatexM');
